package br.eti.busarello;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractCellEditor;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class LabelTableColumn extends AbstractCellEditor implements TableCellRenderer, TableCellEditor{
	private static final long serialVersionUID = 1L;

	private JLabel labelRenderer;
	private JLabel labelEdit;

	private ActionListener actionListener;

	private TableColumn tableColumn;
	
	public static LabelTableColumn install(JTable table, int columnIndex){
		return new LabelTableColumn(table, columnIndex);
	}
	
	public static LabelTableColumn install(JTable table, TableColumn tableColumn){
		return new LabelTableColumn(table, tableColumn);
	}
	
	private LabelTableColumn(JTable table, int columnIndex) {
		this(table, table.getColumnModel().getColumn(columnIndex));
	}
	
	private LabelTableColumn(final JTable table, TableColumn tableColumn) {
		super();
		this.tableColumn = tableColumn;
		
		table.addMouseMotionListener(new MouseAdapter() {

			@Override
			public void mouseMoved(MouseEvent e) {

				int column = table.columnAtPoint(e.getPoint());

				if (LabelTableColumn.this.tableColumn.getModelIndex() == column) {
					table.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
				} else {
					table.setCursor(Cursor.getDefaultCursor());
				}
			}
		});

		labelRenderer = new JLabel();
		labelRenderer.setOpaque(true);
		labelRenderer.setHorizontalAlignment(JLabel.CENTER);
		
		labelEdit = new JLabel();		
		labelEdit.setOpaque(true);
		labelEdit.setHorizontalAlignment(JLabel.CENTER);
		
		labelEdit.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
			  mousePressedInternal(e);
			}
		});

		
		tableColumn.setCellRenderer(this);
		tableColumn.setCellEditor(this);

	}

	public void setIcon(Icon icon){
		labelEdit.setIcon(icon);
		labelRenderer.setIcon(icon);
	}
	
	public TableColumn getColumn() {
		return tableColumn;
	};

	public void setActionListener(ActionListener actionListener) {
		this.actionListener = actionListener;
	}

	@Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

		labelRenderer.setForeground(table.getForeground());
		labelRenderer.setBackground(table.getBackground());
						
		if (isSelected) {
			labelRenderer.setForeground(table.getSelectionForeground());
			labelRenderer.setBackground(table.getSelectionBackground());
		}

		labelRenderer.setText((value == null) ? "" : value.toString());
		
		labelRenderer.setEnabled(table.isCellEditable(row, column));
		
		return labelRenderer;
	}

	@Override
  public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
		String text = (value == null) ? "" : value.toString();
		
		labelEdit.setText(text);
		
		labelEdit.setEnabled(table.isCellEditable(row, column));
		
		return labelEdit;
	}

//	@Override
//	public boolean isCellEditable(EventObject e) {		
//	  return super.isCellEditable(e);
//	}
	
	@Override
  public Object getCellEditorValue() {
		return labelEdit.getText();
	}

  public void mousePressedInternal(MouseEvent mouseEvent) {
  	ActionEvent e = new ActionEvent(mouseEvent.getComponent(), mouseEvent.getID(), String.valueOf(mouseEvent.getID()));
  	fireEditingStopped();
  	if(actionListener != null){
  		actionListener.actionPerformed(e);
  	}
	}
}
